<template>
  <div class="register-page">
    <div class="register-container">
      <div class="logo">
        <h1>AI智慧备课系统</h1>
        <p>连接知识与未来</p>
      </div>
      <form
        class="register-form"
        @submit.prevent="handleSubmit"
      >
        <div class="form-group">
          <i class="fas fa-user-astronaut" />
          <input
            v-model="form.username"
            type="text"
            placeholder="请输入手机号"
            required
            @input="clearError('username')"
          >
          <div
            v-if="errors.username"
            class="error-message"
          >
            {{ errors.username }}
          </div>
        </div>

        <div class="form-group">
          <i class="fas fa-lock" />
          <input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            required
            @input="clearError('password')"
          >
          <div
            v-if="errors.password"
            class="error-message"
          >
            {{ errors.password }}
          </div>
        </div>

        <div class="form-group">
          <i class="fas fa-lock" />
          <input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请确认密码"
            required
            @input="clearError('confirmPassword')"
          >
          <div
            v-if="errors.confirmPassword"
            class="error-message"
          >
            {{ errors.confirmPassword }}
          </div>
        </div>

        <div class="form-group">
          <i class="fas fa-envelope" />
          <input
            v-model="form.email"
            type="email"
            placeholder="请输入邮箱"
            required
            @input="clearError('email')"
          >
          <div
            v-if="errors.email"
            class="error-message"
          >
            {{ errors.email }}
          </div>
        </div>

        <button
          type="submit"
          class="register-btn"
          :disabled="isSubmitting"
        >
          <span v-if="!isSubmitting">注 册</span>
          <span v-else>注册中</span>
        </button>
        <div class="login-section">
          <span>已有账号?</span>
          <button
            type="button"
            class="login-btn"
            @click="goToLogin"
          >
            登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
// 移除未使用的onMounted导入
import { ref } from 'vue';  // 仅保留需要的ref
import { useRouter } from 'vue-router';

// 表单数据
const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
});

const isSubmitting = ref(false);
const errors = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
});

const router = useRouter();

// 清除错误信息
const clearError = (field) => {
  errors.value[field] = '';
};

// 表单验证
const validateForm = () => {
  let isValid = true;

  // 用户名验证
  if (!form.value.username.trim()) {
    errors.value.username = '请输入用户名';
    isValid = false;
  } else if (!/^(1[3-9]\d{9}|[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|\d{6,})$/.test(form.value.username)) {
    errors.value.username = '请输入正确的手机号/邮箱';
    isValid = false;
  }

  // 密码验证
  if (!form.value.password) {
    errors.value.password = '请输入密码';
    isValid = false;
  } else if (form.value.password.length < 6) {
    errors.value.password = '密码长度至少6位';
    isValid = false;
  }

  // 确认密码验证
  if (form.value.password !== form.value.confirmPassword) {
    errors.value.confirmPassword = '两次输入的密码不一致';
    isValid = false;
  }

  // 邮箱验证
  if (!form.value.email) {
    errors.value.email = '请输入邮箱';
    isValid = false;
  } else if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(form.value.email)) {
    errors.value.email = '请输入正确的邮箱地址';
    isValid = false;
  }

  return isValid;
};

// 提交表单
const handleSubmit = async () => {
  if (!validateForm()) return;

  isSubmitting.value = true;

  try {
    // 模拟API调用
    await register();

    alert('注册成功！请登录');
    // 实际使用时应该跳转到登录页面
    goToLogin();
  } catch (error) {
    alert('注册失败，请稍后重试');
  } finally {
    isSubmitting.value = false;
  }
};

// 模拟注册API
const register = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // 模拟校验，实际应该替换为真实API调用
      if (form.value.username && form.value.password && form.value.email) {
        resolve();
      } else {
        reject(new Error('注册失败'));
      }
    }, 1000);
  });
};

// 跳转登录页面
const goToLogin = () => {
  router.push('/login');
};
</script>

<script>
export default {
  name: 'UserRegister'  // 已经修改为多词组件名
}
</script>

<style scoped>
.register-page {
  margin: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.9)),
  url('https://images.unsplash.com/photo-1451187580459-43490279c0fa?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2072&q=80');
  background-size: cover;
  background-position: center;
}

.register-container {
  width: 100%;
  max-width: 420px;
  background: rgba(30, 41, 59, 0.8);
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(99, 102, 241, 0.3);
  padding: 40px 30px;
  backdrop-filter: blur(8px);
  border: 1px solid rgba(99, 102, 241, 0.2);
  animation: fadeIn 0.6s ease-out;
  position: relative;
  overflow: hidden;
}

.register-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.1) 0%, transparent 70%);
  z-index: -1;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h1 {
  color: white;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #e2e8f0, #6366f1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo p {
  color: #94a3b8;
  font-size: 14px;
  letter-spacing: 0.5px;
}

.register-form .form-group {
  position: relative;
  margin-bottom: 25px;
}

.register-form .form-group i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #64748b;
  font-size: 18px;
  transition: all 0.3s;
}

.register-form input {
  width: 100%;
  padding: 15px 15px 15px 5px;
  background: rgba(15, 23, 42, 0.7);
  border: 2px solid #334155;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s;
  outline: none;
  color: white;
}

.register-form input::placeholder {
  color: #64748b;
}

.register-form input:focus {
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.3);
}

.register-form input:focus + i {
  color: #6366f1;
}

.register-form input.error {
  border-color: #ef4444;
}

.register-btn {
  width: 100%;
  padding: 15px;
  background: #6366f1;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
}

.register-btn:hover {
  background: #4f46e5;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(99, 102, 241, 0.4);
}

.register-btn:active {
  transform: translateY(0);
}

.register-btn::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.3) 0%, transparent 70%);
  opacity: 0;
  transition: opacity 0.5s;
}

.register-btn:hover::after {
  opacity: 1;
}

.register-btn:disabled {
  opacity: 0.8;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.error-message {
  color: #ef4444;
  font-size: 13px;
  margin-top: 5px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-container {
    padding: 30px 20px;
    width: 90%;
  }
}

/* 新增登录相关样式 */
.login-section {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid rgba(148, 163, 184, 0.2);
}

.login-section span {
  color: #94a3b8;
  font-size: 14px;
  margin-right: 10px;
}

.login-btn {
  background: transparent;
  color: #818cf8;
  border: none;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.3s;
}

.login-btn:hover {
  background: rgba(99, 102, 241, 0.1);
  color: #6366f1;
  transform: translateY(-2px);
}

.login-btn:active {
  transform: translateY(0);
}

.login-btn i {
  font-size: 12px;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }
}
</style>